// #include <cpr/cpr.h>
#include <iostream>

#include <ut/dbus.h>

#include <string.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>

using namespace std;
// using namespace cpr;
// using namespace ssl;

int event_process(int num_msg, const struct pam_message **msg,
                  struct pam_response **resp, void *appdata_ptr)
{
    std::cout << "num_msg:" << num_msg << ",msg:" << (*msg)->msg << ",msg_style:" << (*msg)->msg_style << ",resp:" << *resp << std::endl;
    *resp = (struct pam_response *)malloc(num_msg * sizeof(struct pam_response));
    for (int i = 0; i < num_msg; i++) {
        if ((*msg)[i].msg_style == PAM_PROMPT_ECHO_OFF) {
            // (*resp)[i].resp = strdup("1");
            static int tmp = 0;
            // (*resp)[i].resp = strdup(tmp == 0 ? "2" : "1");
            resp[i]->resp = strdup(readline((*msg)[i].msg));

            tmp++;
        } else {
            resp[i]->resp = strdup("test");
        }
        resp[i]->resp_retcode = 0;
        std::cout << "resp_retcode:" << resp[i]->resp_retcode << ",resp:" << resp[i]->resp << std::endl;
    }

    return PAM_SUCCESS;
}

int main(int argc, char *argv[])
{
    // SslOptions ssl = Ssl(VerifyPeer(false));
    // // 请求域管服务端的 HTTPS 接口
    // Response r = Get(Url("https://www.platform-udcp.com/uim/v3/client/system_config/field_rules/name_pc_by_district"), ssl);
    // // 打印服务端响应结果
    // cout << r.text << endl;
    struct pam_conv pam_conversation {
        event_process, NULL
    };
    int status;
    pam_handle_t *pamh;
    pam_start("passwd", "test",
              &pam_conversation,
              &pamh);
    PAM_SYSTEM_ERR;

    // status = setenv("test", "1234", 1);
    // status = pam_chauthtok(pamh, 0);
    system("export test=1234");
    std::cout << "status:" << status << std::endl;
    char *service = NULL;
    std::cout << getenv("test") << std::endl;
    std::cout << getenv("test") << std::endl;
    // status = pam_chauthtok(pamh, 0);
    std::cout << "status:" << status << service << std::endl;
    pam_end(pamh, status);

    return 0;
}
